﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using LiveCharts.Wpf;
using LiveCharts;
using Separator = LiveCharts.Wpf.Separator;
using System.Windows.Threading;

namespace QuickStart_Project_01.Chart
{
    /// <summary>
    /// LiveChart.xaml 的交互逻辑
    /// </summary>
    public partial class LiveChartView : UserControl
    {
        DispatcherTimer timer = new DispatcherTimer();

        public SeriesCollection LineSeriesCollection { get; set; }
        public AxesCollection LineAxisX { get; set; }
        public AxesCollection LineAxisY { get; set; }
        public LiveChartView()
        {
            InitializeComponent();

            LineSeriesCollection = new SeriesCollection
            {
                new LineSeries
                {
                    Title = "销售量",
                    Values = new ChartValues<int> { 5, 8, 10, 12, 14 },
                    PointGeometry = DefaultGeometries.Circle,
                    PointGeometrySize = 15,
                    StrokeThickness = 2,
                    Fill = new SolidColorBrush(Color.FromArgb(50, 33, 149, 242)),
                    LineSmoothness = 0.8, // 曲线平滑度
                    DataLabels = true,
                    LabelPoint = point => $"{point.Y}万个"
                },
                new LineSeries
                {
                    Title = "进货量",
                    Values = new ChartValues<int> { 15, 4, 5, 6, 24 },
                    PointGeometry = DefaultGeometries.Circle,
                    PointGeometrySize = 15,
                    StrokeThickness = 2,
                    Fill = new SolidColorBrush(Color.FromArgb(50,255, 150, 22)),
                    LineSmoothness = 0.8, // 曲线平滑度
                    DataLabels = true,
                    LabelPoint = point => $"{point.Y}万个"
                },
                new LineSeries
                {
                    Title = "库存量",
                    Values = new ChartValues<int> { 25, 23,6, 16, 34 },
                    PointGeometry = DefaultGeometries.Circle,
                    PointGeometrySize = 15,
                    StrokeThickness = 2,
                    Fill = new SolidColorBrush(Color.FromArgb(50, 72, 232, 120)),
                    LineSmoothness = 0.8, // 曲线平滑度
                    DataLabels = true,
                    LabelPoint = point => $"{point.Y}万个"
                }
            };

            LineAxisX = new AxesCollection
            {
                new Axis
                {
                    Title = "月份",
                    Labels = new[] { "一月", "二月", "三月", "四月", "五月","六月","七月","八月","九月","十月","十一月","十二月" },
                    Separator = new Separator { Step = 1, IsEnabled = true }
                }
            };

            LineAxisY = new AxesCollection
            {
                new Axis
                {
                    Title = "销售额（万元）",
                    LabelFormatter = value => value.ToString("N")
                }
            };

            timer.Interval = TimeSpan.FromSeconds(1);
            timer.Tick += (s, e) =>
            {
                var random = new Random();
                LineSeriesCollection[0].Values.Add(random.Next(0, 100));
                if (LineSeriesCollection[0].Values.Count > 12)
                    LineSeriesCollection[0].Values.RemoveAt(0);


                LineSeriesCollection[1].Values.Add(random.Next(0, 100));
                if (LineSeriesCollection[1].Values.Count > 12)
                    LineSeriesCollection[1].Values.RemoveAt(0);


                LineSeriesCollection[2].Values.Add(random.Next(0, 100));
                if (LineSeriesCollection[2].Values.Count > 12)
                    LineSeriesCollection[2].Values.RemoveAt(0);
            };
            timer.Start();

            DataContext = this;
        }
    }
}
